package com.example.comprehension.fragment;

import com.example.comprehension.R;
import com.example.comprehension.R.layout;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SeetingMoreFragment extends Fragment {
	private static SeetingMoreFragment instance;

	public static Fragment getInstance() {
		if (instance == null) {
			instance = new SeetingMoreFragment();
		}
		return instance;
	}

	private ListView lv;
	private List<Map<String,Object>> dataList;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_setting_more, container,
				false);
		lv = (ListView) view.findViewById(R.id.lv);

		initData();
		lv.setAdapter(new AppAdapter());
		return view;
	}

	private void initData() {
		dataList = new ArrayList<Map<String, Object>>();
		dataList.add(new HashMap<String, Object>(){{
			put("name","学霸之战");
			put("desc","完全免费的竞技类APP,边玩边学习，边交朋友。\n" +
					"学习是要有乐趣的，否则为什么要学习。\n" +
					"来\"学霸之战\"赢得你的学霸勋章，学霸限量手表和学霸奖学金吧");
			put("icon",R.drawable.more_icon1);
			put("url","http://pkg.fir.im/f276a4262c3a09b34c268e316c8b546751bdee04.apk?attname=comprehension-debug.apk_1.0.apk&e=1455885764&token=LOvmia8oXF4xnLh0IdH05XMYpH6ENHNpARlmPc-T:-9oTQbql6C6Ah8NZTh3BSfd2WzI=");
		}});
		dataList.add(new HashMap<String, Object>(){{
			put("name","听力训练");
			put("desc","本软件是一款强化听力训练的做题软件，含选择和填空。\n" +
					"题目内容由高级教师精心编写，声音由录制中考或高考的老师专门录音制作。\n" +
					"平时多听多练，正式考试时，就不会因为陌生的声音而紧张。熟能生巧，自然而然就能把听力部分做到满分。");
			put("icon",R.drawable.more_icon2);
			put("url","http://pkg.fir.im/f276a4262c3a09b34c268e316c8b546751bdee04.apk?attname=comprehension-debug.apk_1.0.apk&e=1455885764&token=LOvmia8oXF4xnLh0IdH05XMYpH6ENHNpARlmPc-T:-9oTQbql6C6Ah8NZTh3BSfd2WzI=");
		}});
		dataList.add(new HashMap<String, Object>(){{
			put("name","语法训练");
			put("desc","做题模式接近于“学霸之战”APP，游戏关卡形式，3-5分钟一次练习，包含多款道具。\n" +
					"每天用极少量的碎片时间在手机上学习，把不懂的搞懂，成绩自然就会提高。");
			put("icon",R.drawable.more_icon3);
			put("url","http://pkg.fir.im/f276a4262c3a09b34c268e316c8b546751bdee04.apk?attname=comprehension-debug.apk_1.0.apk&e=1455885764&token=LOvmia8oXF4xnLh0IdH05XMYpH6ENHNpARlmPc-T:-9oTQbql6C6Ah8NZTh3BSfd2WzI=");
		}});
		dataList.add(new HashMap<String, Object>(){{
			put("name","完形填空");
			put("desc","“完形填空”是历年来英语考试中最难的一部分，也是失分率最多的题型。\n" +
					"本软件注重所选文章的代表性，包含选择题和填空题，试题设计接近考试，答案解析透彻到位。做题结束，有雷达五边形显示能力强弱，真正做到知己知彼。");
			put("icon",R.drawable.more_icon4);
			put("url","http://pkg.fir.im/f276a4262c3a09b34c268e316c8b546751bdee04.apk?attname=comprehension-debug.apk_1.0.apk&e=1455885764&token=LOvmia8oXF4xnLh0IdH05XMYpH6ENHNpARlmPc-T:-9oTQbql6C6Ah8NZTh3BSfd2WzI=");
		}});
		dataList.add(new HashMap<String, Object>(){{
			put("name","阅读理解");
			put("desc","“阅读理解”是英语考试中必考题型，分值重，耗时多。\n" +
					"看不懂文章，常常是失分的最大原因。\n" +
					"本软件遵循“精、透”原则，每篇文章包含中文翻译，答案解析透彻到位。\n" +
					"当正确运用解题方法和技巧，你会发现阅读理解也许是最简单的一部分。 ");
			put("icon",R.drawable.more_icon5);
			put("url","http://pkg.fir.im/f276a4262c3a09b34c268e316c8b546751bdee04.apk?attname=comprehension-debug.apk_1.0.apk&e=1455885764&token=LOvmia8oXF4xnLh0IdH05XMYpH6ENHNpARlmPc-T:-9oTQbql6C6Ah8NZTh3BSfd2WzI=");
		}});
	}

	class AppAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return dataList.size();
		}

		@Override
		public Map<String,Object> getItem(int position) {
			return dataList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = View.inflate(getActivity(), R.layout.list_item_app_download,null);
			}
			TextView tv_name = (TextView) convertView.findViewById(R.id.tv_name);
			TextView tv_desc = (TextView) convertView.findViewById(R.id.tv_desc);
			ImageView iv_icon = (ImageView) convertView.findViewById(R.id.iv_icon);
			View btn = convertView.findViewById(R.id.iv_download);

			Map<String,Object> map = getItem(position);
			tv_name.setText((String) map.get("name"));
			tv_desc.setText((String) map.get("desc"));
			iv_icon.setImageResource((Integer) map.get("icon"));
			final String url = (String) map.get("url");
			btn.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = new Intent();
					intent.setAction("android.intent.action.VIEW");
					intent.setData(Uri.parse(url));
					startActivity(intent);
				}
			});
			return convertView;
		}
	}
}
